<?php 

namespace Home\Controller;
use Think\Controller;
class SetController extends CommonController {
    public function index(){

       
        $id = $_SESSION['id'];
        $res = M('user')->where(array('id'=>$id))->find();

        $this->assign('title','设置');

       
        $this->assign('res',$res);
       
        $this->display();
       
        
    }
    public function checkdata()
    {

    	$email = I('post.email');

    	$id = $_SESSION['id'];


    	$sql = "select * from user where email='{$email}' and id <> '{$id}' ";
    	
    	if (M()->query($sql)) {
              $arr = array(
                'error' => 1,
                'info' => '邮箱已经存在'
                );
            echo json_encode($arr);
            exit;
        }

        $nickname = I('post.nickname');

        $sql = "select * from user where nickname='{$nickname}' and id <> '{$id}' ";

        if (M( )->query($sql)) {
              $arr = array(
                'error' => 1,
                'info' => '昵称已经存在'
                );
            echo json_encode($arr);
            exit;
        }    
    	$res =I('post.');
    	$arr = M('User')->where(array('id'=>$_SESSION['id']))->save($res);
    	if ($arr) {
            $_SESSION['nickname'] = I('post.nickname');
            $_SESSION['city'] = I('post.city');
    		$arr = array(
                'error' => 0,
                'info' => '更改成功'
                );
            echo json_encode($arr);
            exit;
        }else{
             $arr = array(
                'error' => 1,
                'info' => '更改失败'
                );
            echo json_encode($arr);
        }
       
    }
     public function face($value='')
    {
        $this->assign('title','设置');
    	$this->display();
    }
    public function upload()
    {
    	
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
    $upload->savePath  =     ''; // 设置附件上传（子）目录
    // 上传文件 
    $info   =   $upload->upload();
    var_dump($info);
    if(!$info) {// 上传错误提示错误信息
     echo $upload->getError();
    }else{// 上传成功

	    	$info = current($info);

	    	$sql = "uploads/".$info['savepath'].$info['savename'];

	    	echo $sql;

	    	$res = M('user')->where(array('id'=>$_SESSION['id']))->save(array('face'=>$sql));

	    	if ($res) {
                $_SESSION['face']=$sql;
	    		echo '上传成功';
	    	}else{
	    		echo 0;
	    	}
		}
    }
   
    public function checkdate()
        {
            $id = $_SESSION['id'];
            $res = M('user')->where(array('id'=>$id))->find();

            // var_dump(I('post.'));

            $nowpassword = I('post.nowpassword');
            $repassword = I('post.repassword');
            $password = I('post.password');
            if ($nowpassword==$password) {
               $arr = array('error'=>1,'info'=>'新密码为当前密码，请重新修改');
                echo json_encode($arr);
                exit;
            }

            $nowpassword = md5($nowpassword);
           
            // var_dump($password);
            // var_dump($res['password']);
            if ($nowpassword!=$res['password']) {
            $arr = array('error'=>1,'info'=>'当前密码错误');
            echo json_encode($arr);
            exit;
            }
            if ($repassword!=$password) {
                $arr = array('error'=>1,'info'=>'密码不一致');
                echo json_encode($arr);
                exit;
            }
            $ree['password'] = md5(I('post.password'));
            
            // $ree = md5($ree);
            $arr = M('User')->where(array('id'=>$id))->save($ree);

            if ($arr) {
               // $res['password'] = I('post.password');
               $arr = array(
                'error' => 0,
                'info' => '更改成功'
                );
                echo json_encode($arr);
                exit;
                }else{
                     $arr = array(
                        'error' => 1,
                        'info' => '更改失败'
                        );
                    echo json_encode($arr);
                }
            }
            
        
         public function mma()
        {
       
            $this->assign('title','设置');
         $this->display();
        }


        public function sandemail()
        {
           $this->xbs('1127640404@qq.com','玩一下','装装逼');
        }
        public function zx()
        {

            $id = $_SESSION['id'];
            $sql = "select * from question  where uid={$id}";

            $res = M()->query($sql);

            $this->assign('res',$res);
           $this->display();
        }

    }















 ?>